﻿using Application.Purchase_hService;
using AutoMapper;
using Domain.Entity;
using Domain.Entity.ord;
using Domain.Entity.search;
using Domain.IRepository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Application.ISale_hService
{
    public class Sale_hService : ISale_hService
    {
        private readonly ISale_hRepository sale_HRepository;
        private readonly IMapper mapper;

        public Sale_hService(ISale_hRepository sale_HRepository,IMapper mapper)
        {
            this.sale_HRepository = sale_HRepository;
            this.mapper = mapper;
        }


        //添加销售单
        public async Task<int> AddSale_h(AddSale_hDto addSale_HDto)
        {
            //转换
            Sale_h sale_H = mapper.Map<Sale_h>(addSale_HDto);
            return await sale_HRepository.AddSale_h(sale_H);
        }

        //审核销售单
        public async Task<int> ApproverSale_h(int saleid, int status, string approvername, string approvertime)
        {
            return await sale_HRepository.ApproverSale_h(saleid, status, approvername, approvertime);
        }

        //删除销售单
        public async Task<int> DeleteSale_h(int id)
        {
            return await sale_HRepository.DeleteSale_h(id);
        }

        //高级查询
        public async Task<FenYe<Sale_h>> GetAdvancedSale_h(FenYe<Sale_h> fenYe, string? code, string? starttotal, string? endtotal, string? startdate, string? enddate, string? startnum, string? endnum, int? status, int? cust, string? custlink, int? dep, int? user, string? createname, string? startcreatetime, string? endcreatetime, string? approvername, string? startapprovertime, string? endapprovertime)
        {
            return await sale_HRepository.GetAdvancedSale_h(fenYe, code, starttotal, endtotal, startdate, enddate, startnum, endnum, status, cust, custlink, dep, user, createname, startcreatetime, endcreatetime, approvername,startapprovertime,endapprovertime);
        }


        //根据货品id查询销售记录
        public async Task<List<Sale_h>> GetAllSale_hBypid(int procid)
        {
            return await sale_HRepository.GetAllSale_hBypid(procid);
        }


        //根据销售编号查询主键id和销售总数
        public async Task<Sale_h> Getid(string code)
        {
            return await sale_HRepository.Getid(code);
        }

        //分页查询销售单
        public async Task<FenYe<Sale_h>> GetSale_h(FenYe<Sale_h> fenYe, string? code, int? status, int? cust, string? starttime, string? endtime)
        {
            return await sale_HRepository.GetSale_h(fenYe,code,status,cust,starttime,endtime);
        }


        //根据id查询销售单
        public async Task<Sale_h> GetSale_HById(int id)
        {
            return await sale_HRepository.GetSale_HById(id);
        }


        //查询所有销售单
        public async Task<List<Sale_h>> GetSale_Hs()
        {
            return await sale_HRepository.GetSale_Hs();
        }


        //销售统计
        public async Task<SaleTJResult> Sale_hTJ(string? timeType, string? code, int? cust, int? user, int? dep, string? starttime, string? endtime, int? page, int? pagesize)
        {
            return await sale_HRepository.Sale_hTJ(timeType,code,cust,user,dep,starttime,endtime,page,pagesize);
        }


        //更新出库数量及状态
        public async Task<int> UpdateNum(int saleid, int oknum, int nonum, int statu)
        {
            return await sale_HRepository.UpdateNum(saleid, oknum, nonum, statu);
        }

        //更新退货数量及状态
        public async Task<int> UpdateRNum(int saleid, int returnnum, int status)
        {
            return await sale_HRepository.UpdateRNum(saleid, returnnum, status);
        }

        public async Task<int> UpdateRNum(int saleid, int returnnum, int status, int newnum)
        {
            return await sale_HRepository.UpdateRNum(saleid,returnnum,status,newnum);
        }


        //修改销售单
        public async Task<int> UpdateSale_h(UpdateSale_hDto updateSale_HDto)
        {
            //转换
            Sale_h sale_H = mapper.Map<Sale_h>(updateSale_HDto);
            return await sale_HRepository.UpdateSale_h(sale_H);
        }


        //修改销售单据状态
        public async Task<int> UpdateStatus(int saleid, int status)
        {
            return await sale_HRepository.UpdateStatus(saleid, status);
        }
    }
}
